/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import Entities.Afiliacion;
import Entities.Afiliado;
import controller.exceptions.NonexistentEntityException;
import controller.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author Nas
 */
public class AfiliacionJpaController implements Serializable {

    public AfiliacionJpaController() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("DataLayerPU");
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Afiliacion afiliacion) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            afiliacion.setIdAfiliacion(getMaxId());
            em.persist(afiliacion);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findAfiliacion(afiliacion.getIdAfiliacion()) != null) {
                throw new PreexistingEntityException("Afiliacion " + afiliacion + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Afiliacion afiliacion) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            afiliacion = em.merge(afiliacion);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = afiliacion.getIdAfiliacion();
                if (findAfiliacion(id) == null) {
                    throw new NonexistentEntityException("The afiliacion with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Afiliacion afiliacion;
            try {
                afiliacion = em.getReference(Afiliacion.class, id);
                afiliacion.getIdAfiliacion();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The afiliacion with id " + id + " no longer exists.", enfe);
            }
            em.remove(afiliacion);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Afiliacion> findAfiliacionEntities() {
        return findAfiliacionEntities(true, -1, -1);
    }

    public List<Afiliacion> findAfiliacionEntities(int maxResults, int firstResult) {
        return findAfiliacionEntities(false, maxResults, firstResult);
    }

    private List<Afiliacion> findAfiliacionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Afiliacion.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Afiliacion findAfiliacion(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Afiliacion.class, id);
        } finally {
            em.close();
        }
    }

    public int getAfiliacionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Afiliacion> rt = cq.from(Afiliacion.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public List<Afiliacion> findByIdAfiliado(Integer idAfiliado) {
        EntityManager em = getEntityManager();
        return em.createNamedQuery("Afiliacion.findByIdAfiliado")
                .setParameter("idAfiliado", idAfiliado).getResultList();
    }

    public List<Afiliacion> findByEstado(String estado) {
        EntityManager em = getEntityManager();
        return em.createNamedQuery("Afiliacion.findByEstado")
                .setParameter("estado", estado).getResultList();
    }

    public List<Afiliacion> findByEstadoAndAfiliado(Integer idAfiliado, String estado) {
        EntityManager em = getEntityManager();
        return em.createNamedQuery("Afiliacion.findByEstadoAndAfiliado")
                .setParameter("idAfiliado", idAfiliado).setParameter("estado", estado).getResultList();
    }

    private Integer getMaxId() {
        EntityManager em = getEntityManager();
        Integer maxId = (Integer) em.createNamedQuery("Afiliacion.maxId").getSingleResult();
        if (maxId != null) {
            maxId = maxId + 1;
        } else {
            maxId = 1;
        }
        return maxId;
    }
}
